package com.hnust.controller;

import com.hnust.controller.paper.paperManagement.PaperListController;
import com.hnust.fx.AbstractFxmlView;
import com.hnust.fx.FXMLController;
import com.hnust.service.TestPaperService;
import com.hnust.store.DataStore;
import com.hnust.view.PopupView;
import com.hnust.view.paper.paperManagement.*;
import com.hnust.view.paper.autoPaper.AutoPaper2View;
import com.hnust.view.paper.autoPaper.AutoPaper3View;
import com.hnust.view.paper.autoPaper.AutoPaper4View;
import com.hnust.view.paper.autoPaper.AutoPaperView;
import com.hnust.view.paper.generatePaperView.GeneratePaperSecondView;
import com.hnust.view.paper.generatePaperView.GeneratePaperThirdView;
import com.hnust.view.paper.generatePaperView.GeneratePaperView;
import com.hnust.view.personInfo.ChaperImportView;
import com.hnust.view.personInfo.MyCourseGroupView;
import com.hnust.view.personInfo.PersonDataView;
import com.hnust.view.student.DetailDataView;
import com.hnust.view.student.OverallDataView;
import com.hnust.view.subject.BatchImportView;
import com.hnust.view.subject.SubjectListView;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPopup;
import com.jfoenix.controls.JFXRippler;
import com.jfoenix.controls.JFXScrollPane;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class MainController implements Initializable{
    public StackPane contentWindow;
    public ScrollPane scrollpane1;
    public HBox newContent;
    public VBox leftContent;
    public VBox rightContent;
    public VBox drawerContent;
    public JFXButton header1Item1Button;
    public JFXButton header1Item2Button;
    public JFXButton header1Item3Button;
    public JFXButton header2Item2Button;
    public JFXButton header2Item1Button;
    public JFXButton header3Item1Button;
    public JFXButton header3Item2Button;
    public JFXButton header4Item1Button;
    public JFXButton header4Item2Button;
    public JFXButton header4Item3Button;
    public JFXButton header4Item4Button;
    public JFXButton header4Item5Button;
    public JFXButton header4Item6Button;
    public JFXButton header4Item7Button;
    public AnchorPane bigContent;
    public JFXRippler rippler;
    public Label teacherName;
    @FXML
    public Label navFont1;
    @FXML
    private Label navFont;
    @FXML
    private AnchorPane content;
    @Autowired
    private DataStore dataStore;
    @Autowired
    private TestPaperService testPaperService;
    public ImageView header1Item1Image;
    public ImageView header1Item2Image;
    public ImageView header1Item3Image;
    public ImageView header2Item1Image;
    public ImageView header2Item2Image;
    public ImageView header3Item2Image;
    public ImageView header3Item1Image;
    public ImageView header4Item1Image;
    public ImageView header4Item2Image;
    public ImageView header4Item3Image;
    public ImageView header4Item4Image;
    public ImageView header4Item5Image;
    public ImageView header4Item6Image;
    public ImageView header4Item7Image;
    @Autowired
    private GeneratePaperView generatePaperView;
    @Autowired
    private PaperListView paperListView;
    @Autowired
    private MyCourseGroupView myCourseGroupView;
    @Autowired
    private ChaperImportView chaperImportView;
    @Autowired
    private PersonDataView personDataView;
    @Autowired
    private DetailDataView detailDataView;
    @Autowired
    private OverallDataView overallDataView;
    @Autowired
    private BatchImportView batchImportView;
    @Autowired
    private SubjectListView subjectListView;
    @Autowired
    private AutoPaperView autoPaperView;
    @Autowired
    private GeneratePaperSecondView generatePaperSecondView;
    @Autowired
    private GeneratePaperThirdView generatePaperThirdView;
    @Autowired
    private AutoPaper2View autoPaper2View;
    @Autowired
    private AutoPaper3View autoPaper3View;
    @Autowired
    private AutoPaper4View autoPaper4View;
    @Autowired
    private PopupView popupView;
    @Autowired
    private PaperPreviewView paperPreviewView;
    @Autowired
    private PaperReviewView paperReviewView;
    @Autowired
    private PaperCorrectView paperCorrectView;
    @Autowired
    private PaperResultView paperResultView;

    private static final Logger logger = LogManager.getLogger(MainController.class);

    @SneakyThrows
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("调用init");
        listenChange();
        scrollpane1.setFitToWidth(true);
        scrollpane1.setFitToHeight(true);
        //是否可以用鼠标移动
        scrollpane1.setPannable(true);

        scrollpane1.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        scrollpane1.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);

        contentWindow.setPrefHeight(dataStore.getScreenHeight()*0.93);
        contentWindow.setPrefWidth(dataStore.getScreenWidth()*0.98);
        rightContent.prefWidthProperty().bind(newContent.widthProperty().subtract(leftContent.getPrefWidth()));
        drawerContent.setPrefWidth(leftContent.getPrefWidth());
        bigContent.prefWidthProperty().bind(rightContent.widthProperty());
        bigContent.prefHeightProperty().bind(rightContent.heightProperty().subtract(100));
        contentWindow.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                //说明被挂载
                skipView("个人资料");
                teacherName.setText(dataStore.getTeacher_name());
                logger.info("用户进入主界面："+dataStore);
            }
        });
    }

    public void skipView(String text) {
        //设置全部隐藏
        setAllItemImageInvisible();
        setAllItemButtonAble();
        AbstractFxmlView path=null;
        String label=null;
        String mainlabel=null;
        content.setStyle("-fx-background-color: #ffffff");
        switch (text) {
            case "个人资料":
                header1Item1Image.setVisible(true);
                header1Item1Button.setDisable(true);
                label="个人信息管理  /  个人资料";
                mainlabel="个人资料";
                content.setStyle("-fx-background-color: #99cdff");
                path = personDataView;
                break;
            case "我的课程组":
                header1Item2Image.setVisible(true);
                header1Item2Button.setDisable(true);
                path = myCourseGroupView;
                label="个人信息管理  /  我的课程组";
                mainlabel="我的课程组";
                break;
            case "批量导入章节":
                header1Item3Image.setVisible(true);
                header1Item3Button.setDisable(true);
                path = chaperImportView;
                label="个人信息管理  /  批量导入章节";
                break;
            case "题目列表":
                header2Item1Image.setVisible(true);
                header2Item1Button.setDisable(true);
                label="题目管理  /  题目列表";
                mainlabel="题目列表";
                path = subjectListView;
                break;
            case "批量导入":
                header2Item2Image.setVisible(true);
                header2Item2Button.setDisable(true);
                path = batchImportView;
                label="题目管理  /  批量导入";
                mainlabel="批量导入";
                break;
            case "总体数据查看":
                header3Item1Image.setVisible(true);
                header3Item1Button.setDisable(true);
                path = overallDataView;
                label="学生管理  /  总体数据查看";
                mainlabel="总体数据查看";
                break;
            case "详细数据查看":
                header3Item2Image.setVisible(true);
                header3Item2Button.setDisable(true);
                path = detailDataView;
                label="学生管理  /  详细数据查看";
                mainlabel="详细数据查看";
                break;
            case "试卷列表":
                header4Item1Image.setVisible(true);
                header4Item1Button.setDisable(true);
                path = paperListView;
                label="试卷管理  /  试卷列表";
                mainlabel="试卷列表";
                break;
            case "预览试卷":
                header4Item4Image.setVisible(true);
                header4Item4Button.setDisable(true);
                path = paperPreviewView;
                label="试卷管理  /  预览试卷";
                mainlabel="预览试卷";
                break;
            case "试卷批阅":
                header4Item5Image.setVisible(true);
                header4Item5Button.setDisable(true);
                path = paperReviewView;
                label="试卷管理  /  试卷批阅";
                mainlabel="试卷批阅";
                break;
            case "批阅结果":
                header4Item6Image.setVisible(true);
                header4Item6Button.setDisable(true);
                path = paperResultView;
                label="试卷管理  /  批阅结果";
                mainlabel="批阅结果";
                break;
            case "试卷修改":
                header4Item7Image.setVisible(true);
                header4Item7Button.setDisable(true);
                path = paperCorrectView;
                label="试卷管理  /  试卷修改";
                mainlabel="试卷修改";
                break;
            case "手动生成试卷":
                header4Item2Image.setVisible(true);
                header4Item2Button.setDisable(true);
                path = generatePaperView;
                label="试卷管理  /  手动生成试卷  /  设置试卷基本信息";
                mainlabel="设置试卷基本信息";
                break;
            case "手动生成试卷II":
                header4Item2Image.setVisible(true);
                header4Item2Button.setDisable(true);
                path = generatePaperSecondView;
                label="试卷管理  /  手动生成试卷  /  选择题目";
                mainlabel="选择题目";
                break;
            case "手动生成试卷III":
                header4Item2Image.setVisible(true);
                header4Item2Button.setDisable(true);
                path = generatePaperThirdView;
                label="试卷管理  /  手动生成试卷  /  保存试卷";
                mainlabel="保存试卷";
                break;
            case "自动生成试卷":
                header4Item3Image.setVisible(true);
                header4Item3Button.setDisable(true);
                path = autoPaperView;
                label="试卷管理  /  自动生成试卷  /  设置试卷基本信息";
                mainlabel="设置试卷基本信息";
                break;
            case "自动生成试卷II":
                header4Item3Image.setVisible(true);
                header4Item3Button.setDisable(true);
                path = autoPaper2View;
                label="试卷管理  /  自动生成试卷  /  设置试卷题目信息";
                mainlabel="设置试卷题目信息";
                break;
            case "自动生成试卷III":
                header4Item3Image.setVisible(true);
                header4Item3Button.setDisable(true);
                path = autoPaper3View;
                label="试卷管理  /  自动生成试卷  /  查看试卷题目";
                mainlabel="查看试卷题目";
                break;
            case "自动生成试卷IIII":
                header4Item4Image.setVisible(true);
                header4Item4Button.setDisable(true);
                path = autoPaper4View;
                label="试卷管理  /  自动生成试卷  /  保存试卷";
                mainlabel="保存试卷";
                break;
        }
        navFont.setText(label);
        navFont1.setText(mainlabel);
        assert path != null;
        skipPage(path);
    }

    public void itemClick(ActionEvent mouseEvent) throws JSONException {
        //设置点击的显示
        JFXButton button=(JFXButton) mouseEvent.getTarget();
        System.out.println("mouseEvent.getTarget() : " +mouseEvent.getTarget());
        logger.info("用户点击了："+button.getText());
        skipView(button.getText());
    }

    private void setAllItemImageInvisible() {
       header1Item1Image.setVisible(false);header1Item2Image.setVisible(false);
       header2Item1Image.setVisible(false);header2Item2Image.setVisible(false);
       header3Item1Image.setVisible(false);header3Item2Image.setVisible(false);
       header4Item1Image.setVisible(false);header4Item2Image.setVisible(false);
       header4Item3Image.setVisible(false);header4Item4Image.setVisible(false);
       header4Item5Image.setVisible(false);header4Item6Image.setVisible(false);
       header4Item7Image.setVisible(false);header1Item3Image.setVisible(false);
    }

    private void setAllItemButtonAble(){
        header1Item1Button.setDisable(false);header1Item1Button.setDisableVisualFocus(true);
        header1Item2Button.setDisable(false);header1Item2Button.setDisableVisualFocus(true);
        header1Item3Button.setDisable(false);header1Item3Button.setDisableVisualFocus(true);
        header2Item1Button.setDisable(false);header2Item1Button.setDisableVisualFocus(true);
        header2Item2Button.setDisable(false);header2Item2Button.setDisableVisualFocus(true);
        header3Item1Button.setDisable(false);header3Item1Button.setDisableVisualFocus(true);
        header3Item2Button.setDisable(false);header3Item2Button.setDisableVisualFocus(true);
        header4Item1Button.setDisable(false);header4Item1Button.setDisableVisualFocus(true);
        header4Item2Button.setDisable(false);header4Item2Button.setDisableVisualFocus(true);
        header4Item3Button.setDisable(false);header4Item3Button.setDisableVisualFocus(true);
        header4Item4Button.setDisable(false);header4Item4Button.setDisableVisualFocus(true);
        header4Item5Button.setDisable(false);header4Item5Button.setDisableVisualFocus(true);
        header4Item6Button.setDisable(false);header4Item6Button.setDisableVisualFocus(true);
        header4Item7Button.setDisable(false);header4Item7Button.setDisableVisualFocus(true);
    }

    public void shiftToPersonalData() throws IOException {
        JFXPopup popup=new JFXPopup((Region) popupView.getView());
        popup.show(rippler, JFXPopup.PopupVPosition.TOP,
                JFXPopup.PopupHPosition.RIGHT,-50,40);
//        skipView("个人资料");
    }

    public void skipPage(AbstractFxmlView path){
        content.getChildren().clear();
        assert path != null;
        content.getChildren().add(path.getView());
        AnchorPane.setBottomAnchor(path.getView(),0.0);
        AnchorPane.setTopAnchor(path.getView(),0.0);
        AnchorPane.setLeftAnchor(path.getView(),0.0);
        AnchorPane.setRightAnchor(path.getView(),0.0);
    }
    public void listenChange(){
        //通过监听最外层容器的宽度，来改变内层Anchor的宽度
        scrollpane1.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                newContent.setPrefWidth((double)newValue-2);
            }
        });
        //通过监听最外层容器的高度，来改变内层Anchor的高度
        scrollpane1.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                newContent.setPrefHeight((double)newValue-2);
            }
        });
    }
}
